﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using PlanningGIS.Util;
using System.Drawing;
using System.Windows.Forms;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 绘制文本控件
    /// </summary>
    public partial class CtlDrawText : ButtonX 
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlDrawText()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            this.ColorTable = eButtonColor.Flat;
            this.BackColor = Color.Transparent;
            this.Style = eDotNetBarStyle.StyleManagerControlled;
            AddButton(); ;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container">控件容器</param>
        public CtlDrawText(IContainer container)
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            container.Add(this);

            InitializeComponent();
            AddButton();
        }

        /// <summary>
        /// 地图管理对象，MapControl.Object
        /// </summary>
        public object Hook { get; set; }

        /// <summary>
        /// 获取或设置当前选择的绘制工具对象
        /// </summary>
        public ESRI.ArcGIS.SystemUI.ICommand CurrentCommand { get; set; }

        /// <summary>
        /// 添加按钮
        /// </summary>
        private void AddButton()
        {
            this.Tooltip = "绘制文本";
            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ToolDrawText",Name="添加文本"},
                new ValueName(){Value="ToolDrawTextPath",Name="折线添加文本"}
            };
            foreach (ValueName v in btns)
            {
                ButtonItem btn = new ButtonItem(v.Value, v.Name);
                btn.Tag = GetCommandByName(v.Value);
                string imagename = v.Value.Replace("Tool", "");
                Bitmap bmp = (Bitmap)(Properties.Resources.ResourceManager.GetObject(imagename));
                bmp.MakeTransparent();
                btn.Image = bmp;
                btn.Click += new EventHandler(btn_Click);
                this.SubItems.Add(btn);
            }
            this.Image = ((ButtonItem)this.SubItems[0]).Image;
            this.Tag = ((ButtonItem)this.SubItems[0]).Tag;
            this.Click += new EventHandler(CtlDrawText_Click);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private ESRI.ArcGIS.SystemUI.ICommand GetCommandByName(string name)
        {
            if (name.Equals("ToolDrawText", StringComparison.CurrentCultureIgnoreCase))
            {
                return new ToolDrawText() as ESRI.ArcGIS.SystemUI.ICommand;
            }
            else if (name.Equals("ToolDrawTextPath", StringComparison.CurrentCultureIgnoreCase))
            {
                return new ToolDrawTextPath() as ESRI.ArcGIS.SystemUI.ICommand;
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CtlDrawText_Click(object sender, EventArgs e)
        {
            CurrentCommand = this.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            SelectedChanged();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            this.Image = btn.Image;
            this.Tag = btn.Tag;
            if (btn.Tag == null) return;
            CurrentCommand = btn.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            SelectedChanged();

        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void SelectedChanged()
        {
            if (Hook != null && CurrentCommand != null)
            {
                CurrentCommand.OnCreate(Hook);
                CurrentCommand.OnClick();
            }
        }
    }
}
